package com.ray.friendslist.service;

import java.util.List;

import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Intent;
import android.util.Log;

import com.ray.friendslist.content.ContentManager;
import com.ray.friendslist.content.contract.FriendsContract;
import com.ray.friendslist.content.task.ParamCallback;
import com.ray.friendslist.service.common.CommonService;

public class VKUserProfileService extends CommonService {

	private String USER_ID = "USER_ID";

	@Override
	protected void task(Intent intent) {
		Log.d("ray", "WALL SERVICE STARTED");
		ContentManager.getInstance().getVkWall(
				new ParamCallback<List<JSONObject>>() {

					public void onSuccess(final List<JSONObject> c) {
						Intent intent = new Intent(ACTION_ON_SUCCESS);
						sendBroadcast(intent);
						if (c != null) {
							new Thread(new Runnable() {

								public void run() {
									for (JSONObject j : c) {
										ContentValues contentValues = new ContentValues();
										contentValues.put("data", j.toString());
										getContentResolver()
												.insert(FriendsContract.UserInfoColumns.CONTENT_URI,
														contentValues);
									}
								}
							}).start();
						}
					}

					public void onError(Throwable e) {
						Log.d("ray", "ONERROR");
						Intent intent = new Intent(ACTION_ON_ERROR);
						intent.putExtra(EXTRA_KEY_MESSAGE, e.getMessage());
					}
				}, intent.getStringExtra(USER_ID));
	}

}
